package top.yaofengqiao.springcloudsimple.message.biz.infrastructure.repository;

import org.springframework.stereotype.Repository;
import top.yaofengqiao.springcloudsimple.message.biz.domain.sms.model.entity.SmsRecordEntity;
import top.yaofengqiao.springcloudsimple.message.biz.domain.sms.model.valobj.SmsStatus;
import top.yaofengqiao.springcloudsimple.message.biz.domain.sms.repository.ISmsRecordRepository;
import top.yaofengqiao.springcloudsimple.message.biz.infrastructure.dao.SmsRecordMapper;
import top.yaofengqiao.springcloudsimple.message.biz.infrastructure.po.SmsRecordPO;

import javax.annotation.Resource;

/**
 * @author yfq
 * @date 2024/6/22 11:09
 * @description
 */
@Repository
public class SmsRecordRepository implements ISmsRecordRepository {
    @Resource
    private SmsRecordMapper smsRecordMapper;

    @Override
    public boolean save(SmsRecordEntity smsRecordEntity) {
        SmsRecordPO smsRecordPO = buildSmsRecordPO(smsRecordEntity);
        if (smsRecordEntity == null) return false;

        return smsRecordMapper.insert(smsRecordPO) == 1;
    }

    @Override
    public long saveGetId(SmsRecordEntity smsRecordEntity) {
        SmsRecordPO smsRecordPO = buildSmsRecordPO(smsRecordEntity);
        if (smsRecordEntity == null) return -1;

        int insert = smsRecordMapper.insert(smsRecordPO);
        return insert == 1 ? smsRecordPO.getId() : -1;
    }

    @Override
    public boolean changeStatus(long id, SmsStatus origin, SmsStatus target) {
        return smsRecordMapper.changeStatus(id, origin.getStatus(), target.getStatus()) == 1;
    }

    private SmsRecordPO buildSmsRecordPO(SmsRecordEntity smsRecordEntity) {
        if (smsRecordEntity == null) return null;

        SmsRecordPO smsRecordPO = new SmsRecordPO();
        smsRecordPO.setId(smsRecordEntity.getId());
        smsRecordPO.setPlatform(smsRecordEntity.getPlatform());
        smsRecordPO.setPhone(smsRecordEntity.getPhone());
        smsRecordPO.setContent(smsRecordEntity.getContent());
        smsRecordPO.setStatus(smsRecordEntity.getStatus());
        smsRecordPO.setCreateTime(smsRecordEntity.getCreateTime());
        smsRecordPO.setUpdateTime(smsRecordEntity.getUpdateTime());
        return smsRecordPO;
    }
}
